using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Library.WebControls.SimpleTab {
	[ControlBuilderAttribute(typeof(PlaceHolderControlBuilder))]
	[ToolboxItem(false)]
	[Browsable(false)]
	public class SpTabPage : Control {
		public SpTabPage() {
			_tabMenu = new AMenu(this);// { ID = "tabMenu" };
			_tabContain = new HtmlGenericControl("div");//{ ID = "tabContain" };
			_tabContain.Controls.Add(this);
			_tabContain.Attributes["class"] = "spTabContent";
		}

		internal SpTabControl TabControl;
		public string Name {
			get {
				//return _name;
				return _tabMenu.Attributes["_tabname"];
			}
			set {
				if (String.IsNullOrEmpty(Text))
					Text = value;
				_tabMenu.Attributes["_tabname"] = value;
			}
		}
		private string _text;
		public string Text {
			get {
				if (_text == null) return String.Empty;
				return _text;
			}
			set {
				_text = value;
				//_tabMenu.InnerHtml = "<span><strong><b>" + value + "</b></strong></span>";
			}
		}


		private readonly HtmlAnchor _tabMenu;
		internal HtmlAnchor TabMenu {
			get { return _tabMenu; }
		}

		private readonly HtmlGenericControl _tabContain;
		internal HtmlGenericControl TabContain {
			get { return _tabContain; }
		}
		protected override void Render(HtmlTextWriter writer) {
			if (Visible) {

				base.Render(writer);
			}
		}

		public override bool Visible {
			get {
				return base.Visible;
			}
			set {
				base.Visible = value;
				if (_tabMenu != null) {
					_tabMenu.Visible = value;
				}

				if (_tabContain != null) _tabContain.Visible = value;
			}
		}

		private class AMenu : HtmlAnchor {
			private SpTabPage _page;
			public AMenu(SpTabPage page) {
				_page = page;
			}

			protected override void Render(HtmlTextWriter writer) {
				if (_page.TabControl.ShowCloseButton) {
					_page._tabMenu.InnerHtml = "<span><strong><text>" + _page.Text + "</text><b onclick='" + _page.TabControl.ClientName + ".CloseTab(this)'></b></strong></span>";
				} else {
					_page._tabMenu.InnerHtml = "<span><strong><text>" + _page.Text + "</text><txt></txt></strong></span>";
				}
				base.Render(writer);
			}
		}
	}
}